package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.callback.RxCancellationCallBack
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.dialog.CancellationDialog
import com.compass.doctor.ui.WebDetailActivity
import com.compass.doctor.ui.patient.adapter.PatientPrescriptionRecordAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.doctor.ui.prescription.viewmodel.PrescribeViewModel
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.KEY_RX_TYPE
import com.compass.framework.constant.REFRESH_EXAMINE
import com.compass.framework.constant.REFRESH_MESSAGE
import com.compass.framework.constant.REFRESH_RX
import com.compass.framework.constant.REFRESH_RX_CANCELLATION
import com.compass.framework.constant.RX_DETAILS
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getStringFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 患者处方记录
 * Date:2024/1/26 17:22
 */
class PatientPrescriptionRecordFragment : BaseVBFragment<FragmentBaseListBinding>(),
    OnLoadMoreListener,
    OnRefreshListener {

    private val prescribeViewModel: PrescribeViewModel by viewModels()
    private val patientViewModel: PatientViewModel by viewModels()
    private lateinit var mAdapter: PatientPrescriptionRecordAdapter
    private var mPage = 1
    private lateinit var patientId: String //患者id
    private var rxType: Int = 1 //处方item样式 1是详情  2是chat详情

    private var cancellationDialog: CancellationDialog.Builder? = null
    private var rxCancellationCallBack: RxCancellationCallBack = object : RxCancellationCallBack {
        override fun onCancellationCallBack(orderId: String, suggest: String?) {
            cancellationDialog?.dismissDialog()
            showLoading()
            prescribeViewModel.prescriptionCancel(orderId, suggest)
        }
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)!!
            rxType = it.getInt(KEY_RX_TYPE)!!
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@PatientPrescriptionRecordFragment)
            setOnLoadMoreListener(this@PatientPrescriptionRecordFragment)
        }
        mAdapter = PatientPrescriptionRecordAdapter(rxType)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(top = dpToPx(10), bottom = dpToPx(10))
                setLastBottom(false)
            })
            adapter = mAdapter
        }

        mAdapter.setBottomCallBack(object : OnCustomButtonClick {
            override fun onButtonCallBack(position: Int, msgType: String?) {
                val item = mAdapter.getItem(position)

                when (msgType) {
                    "cancellation" -> {
                        item?.let { details ->
                            cancellationDialog = CancellationDialog.Builder(
                                requireActivity(),
                                details.id,
                                rxCancellationCallBack
                            )
                            cancellationDialog?.show()

                        }
                    }

                    "details" -> {
                        item?.let { details ->
                            if (details.isTransfer=="1"){
                                showToast(getStringFromResource(R.string.prescribed_tips))

                            }else{
                                WebDetailActivity.start(
                                    context = requireActivity(),
                                    url = RX_DETAILS +details.id,
                                    title ="处方详情"
                                )
                            }
                        }


                    }
                }
            }
        })


    }



    override fun initData() {
        getPrescribedList()

        LiveDataBus.get().with(REFRESH_MESSAGE, String::class.java)
            .observe(this) {
                //创建成功刷新
                getPrescribedList()
            }

        prescribeViewModel.cancelLiveData.observe(this) {
            dismissLoading()
            getPrescribedList()

        }
        patientViewModel.patientPrescriptionLiveData.observe(this){
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }


    private fun getPrescribedList() {
        showLoading()
        patientViewModel.getPatientPrescription(mPage,  patientId)

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getPrescribedList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getPrescribedList()
    }
}